﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;

namespace frmPrincipal
{
    public partial class frmConPorCodigo : Form
    {
        private StandardDBConnection standardDBConnection = new StandardDBConnection();
        private string campo;
        private string tabela;

        public frmConPorCodigo(string lbl, string title, string tabela, string campo, int grdX, int grdY)
        {
            Size grdSize = new Size(grdX, grdY - 70);
            Size frmSize = new Size(grdX + 40, grdY + 50);

            InitializeComponent();

            grdConsultaPorCodigo.Size = grdSize;
            this.Size = frmSize;

            lblCodigo.Text = lbl;
            lblCodigo.Location = new Point(lblCodigo.Location.X - lblCodigo.Size.Width, lblCodigo.Location.Y);
            txtCodigo.Width = frmSize.Width - lblCodigo.Location.X - lblCodigo.Size.Width - btnSearch.Width - 40;
            btnSearch.Location = new Point(txtCodigo.Location.X + txtCodigo.Width + 5, btnSearch.Location.Y);

            this.Text = title;
            this.tabela = tabela;
            this.campo = campo;
        }

        //Funções Auxiliares
        private void reSize()
        {
            txtCodigo.Width = this.Size.Width - lblCodigo.Location.X - lblCodigo.Size.Width - btnSearch.Width - 40;
            btnSearch.Location = new Point(txtCodigo.Location.X + txtCodigo.Width + 5, btnSearch.Location.Y);
            grdConsultaPorCodigo.Size = new Size(this.Size.Width - 37, this.Size.Height - 120);
        }

        //Funções do Form
        private void frmConPorCodigo_Resize(object sender, EventArgs e)
        {
            reSize();
        }
        private void frmConPorCodigo_FormClosed(object sender, FormClosedEventArgs e)
        {
            standardDBConnection.cnn.Close();
        }

        //Pesquisa Por Código
        private void PesquisarPorCodigo()
        {
            string sqlCommandText;
            NpgsqlDataAdapter da;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            try
            {
                if (this.tabela == "cliente" || this.tabela == "fornecedor")
                {
                    sqlCommandText = "SELECT * FROM " + tabela + " WHERE " + campo + " = '" + txtCodigo.Text + "' ";
                }
                else
                {
                    sqlCommandText = "SELECT * FROM " + tabela +
                        " WHERE " + campo + " = " + txtCodigo.Text;
                }
                da = new NpgsqlDataAdapter(sqlCommandText, standardDBConnection.cnn);
                ds.Reset();
                da.Fill(ds);
                dt = ds.Tables[0];
                grdConsultaPorCodigo.DataSource = dt;
            }
            catch
            {
                MessageBox.Show("Digite um código válido!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCodigo.Text = "";
                txtCodigo.Focus();
            }
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            PesquisarPorCodigo();            
        }
        private void txtCodigo_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
                PesquisarPorCodigo();
        }

    }
}
